package smashedapples.core
{
	import flash.events.Event;
	
	import mx.events.FlexEvent;
	import mx.events.PropertyChangeEvent;

	
	[Event( name="propertyChange", type="mx.events.PropertyChangeEvent" )]
	

	dynamic public class Model extends NonVisualComponent
	{
		
		protected var creationComplete:Boolean = false;
		
		
		public function Model():void
		{
			super();
			this.addEventListener( FlexEvent.CREATION_COMPLETE, handleCreationCompleteEvent );
		}
		
		protected function handleCreationCompleteEvent( event:FlexEvent ):void
		{	
			this.creationComplete = true;
		}
		
		public function addListener( type:String ):void
		{
			this.addEventListener( type, handleEvent, true );
		}
		
		protected function handleEvent( event:Event ):void
		{
			trace(event.type);
		}
	
		override public function dispatchEvent(event:Event):Boolean
		{	
			if( event is PropertyChangeEvent )
				handlePropertyChangeEvent( event as PropertyChangeEvent );
			
			return super.dispatchEvent(event);
		}
		
		protected function handlePropertyChangeEvent( event:PropertyChangeEvent ):void
		{
			/* var handlerName : String = getPropertyChangeExecuteHandlerName( event );
			
			var rule:BizRule = this.ruleDictionary[ handlerName ];
			if( rule != null )
			{
				rule.execute();
			} */
		}
		
		protected function getPropertyChangeExecuteHandlerName( event:PropertyChangeEvent ):String
		{
			var name:String = "on" + event.kind.substr(0,1).toUpperCase() + event.kind.slice(1);
				name += event.property.substr(0,1).toUpperCase() + event.property.slice(1);
		
			return name;
		}
		
		
		
		private var _listeners:Array = [];
		public function get listeners():Array
		{
			return _listeners;
		}
		public function set listeners( value:Array ):void
		{
			_listeners = value;
			this.addPropertyChanged( "listeners" );
		}
		
		public function onCommitListeners():void
		{
			for each( var type:String in this.listeners )
			{
				this.addListener( type );
			}
		}
		
		
		
		
		
		
	}
}